//
//  YCOrderListViewController.m
//  YCVillageDoctor
//
//  Created by zhangxiaoliang on 2017/7/27.
//  Copyright © 2017年 zhangxiaoliang. All rights reserved.
//

#import "YCOrderListViewController.h"
#import "YCOrderModel.h"
#import "YCMyOrderViewController.h"
#import "MyOrderViewCell.h"

#import "PayOrderViewController.h"
#import "YCReadingModel.h"
static const NSInteger pageNum = 7 ;
@interface YCOrderListViewController ()<UITableViewDelegate,UITableViewDataSource,myOrderViewCellToPayOrderDelegate>
/*
 
 orderStatus 和 O2OFlag 是请求订单列表的两个参数 , 不同的值对应不同的订单类型 ;
 
 */

@property (nonatomic,assign) YCOrderStatusParams orderStatusParams ;

@property (nonatomic,assign) YCO2OFlagParams O2OFlagParams ;
@property(nonatomic,weak)YCMyOrderViewController *allVc;


@property (nonatomic,strong)UITableView *tableView;

@property(nonatomic,strong) YCOrderModel* orderModel;

@property(nonatomic,strong) NSMutableArray <YCOrderModel*> * dataSource ;

@property(nonatomic,assign) NSInteger page;
@end

@implementation YCOrderListViewController
// 数据源的懒加载
-(NSMutableArray*)dataSource
{
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

-(UITableView *)tableView
{
    if (!_tableView) {
        self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64) style:UITableViewStylePlain];
        self.tableView.dataSource = self;
        self.tableView.delegate = self;
        self.tableView.backgroundColor = [UIColor clearColor];
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.view addSubview:self.tableView];
        
    }
    return _tableView;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MyOrderViewCell  *cell = [MyOrderViewCell myOrderViewCellWithTableView:tableView];
    cell.delegate = self;
    cell.order = self.dataSource[indexPath.row];
    cell.selectionStyle= UITableViewCellSelectionStyleNone;
    return cell;
}

-(CGFloat )tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell* cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
    return cell.frame.size.height;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //YCOrderModel *order = self.dataSource[indexPath.row];
    //[order DetailWithOrdelModel:self.dataSource[indexPath.row] WithViewController:self.allVc];
    NSLog(@"点击了详情");
}
- (void)viewDidLoad {
    [super viewDidLoad];
    //数据初始化
    [self initData];
    //界面初始化
    [self createUI];
    [self.tableView registerNib:[UINib nibWithNibName:@"MyOrderViewCell" bundle:nil] forCellReuseIdentifier:@"MyOrderViewCell"];
    [self.tableView.mj_header beginRefreshing];
}
-(void)initData{
    self.page = 1 ;
    //模型实例化
    self.orderModel=[YCOrderModel new];
}
//MARK: - 搭建界面 根据pushType的类型 实例化参数 请求对应的列表数据 ;
-(void)createUI{
    self.view.backgroundColor = [UIColor whiteColor];
    NSString * navigationItemStr;
    switch (self.pushType) {
        case YCORDER_ALL:
            navigationItemStr = @"全部订单" ;
            self.orderStatusParams = ORDERSTATUS_NULL ;
            break;
        case YCORDER_DIDPAY:
            navigationItemStr = @"已付款" ;
            self.orderStatusParams = ORDERSTATUS_ONE ;
            break;
        case YCORDER_WAIT:
            navigationItemStr = @"待付款" ;
            self.orderStatusParams = ORDERSTATUS_ZERO ;
            break;
        case YCORDER_COMPLETED:
            navigationItemStr = @"已完成" ;
            self.orderStatusParams = ORDERSTATUS_TWO ;
            break;
        case YCORDER_CANCEL:
            navigationItemStr = @"已作废" ;
            self.orderStatusParams = ORDERSTATUS_NULL ;
            break;
            
        default:
            break;
    }
    
    self.navigationItem.title = navigationItemStr;
    
    self.tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            self.page=1;
            [self removeNoDataView];
            [self.orderModel sendPushMessageListRequest:self.page OrderStatus:self.orderStatusParams O2OFlag:self.O2OFlagParams Success:^(NSArray* dataArray){
                if (dataArray.count == 0) {
                    [self.dataSource removeAllObjects];
                    [self noDataViewShowWithSuperView:self.view andTitle:@"暂无数据"];
                    [self.view bringSubviewToFront:self.tableView];
                }else{
                    self.dataSource = [NSMutableArray arrayWithArray:dataArray] ;
                }
                [self.tableView reloadData];
                [self.tableView.mj_header endRefreshing];
            } Failure:^(id faulure) {
                if (!self.dataSource.count) {
                    [self noDataViewShowWithSuperView:self.view andTitle:@"暂无数据"];
                    [self.view bringSubviewToFront:self.tableView];
                }
                [self.tableView reloadData];
                [self.tableView.mj_header endRefreshing];
            }];
            //            [self.tableView.mj_header endRefreshing];
        });
    }];
    
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        NSInteger num = self.dataSource.count % pageNum;
        [self removeNoDataView];
        if (0 == num && self.dataSource.count)
        {
            self.page ++;
            [self.orderModel sendPushMessageListRequest:self.page OrderStatus:self.orderStatusParams O2OFlag:self.O2OFlagParams Success:^(NSArray* dataArray){
                [self.dataSource addObjectsFromArray:dataArray];
                [self.tableView reloadData];
                [self.tableView.mj_footer endRefreshing];
            }Failure:^(id failure) {
                [self.tableView.mj_footer endRefreshing];
            }];
        }
        else {
            if(!self.dataSource.count)
            {
                [self noDataViewShowWithSuperView:self.view andTitle:@"暂无数据"];
                [self.view bringSubviewToFront:self.tableView];
            }
            [self.tableView.mj_footer endRefreshing];
            [self presentMessageTips:@"已加载全部信息"];
        }
    }];
}

- (void)myOrderViewCellToPayOrder:(YCOrderModel *)order payType:(NSInteger)type
{
    if (type == YCPUSHTYPE_PAY) { //立即支付
        PayOrderViewController *payVC = [[PayOrderViewController alloc]init];
        YCReadingModel *model = [[YCReadingModel alloc]init];
        model.hosName = order.orderName;
        model.payMoney = [NSString stringWithFormat:@"%.2lf",order.orderMoney];
        model.orderId = order.orderId;
        payVC.readingModel = model;
        payVC.orderId = order.orderId;
        [[NSNotificationCenter defaultCenter]postNotificationName:@"kPushVcNoti" object:payVC];
    }
    else if (type == YCPUSHTYPE_CHECK) //待核销
    {
        NSLog(@"点击了待核销");
    }

}


@end
